//
//  Files.m
//
//  Created by TuanNT on 5/4/14
//  Copyright (c) 2014 Fsoft. All rights reserved.
//

#import "Files.h"


NSString *const kFilesImage = @"image";
NSString *const kFilesCaption = @"caption";


@interface Files ()

- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;

@end

@implementation Files

@synthesize image = _image;
@synthesize caption = _caption;


+ (Files *)modelObjectWithDictionary:(NSDictionary *)dict
{
    Files *instance = [[Files alloc] initWithDictionary:dict];
    return instance;
}

- (instancetype)initWithDictionary:(NSDictionary *)dict
{
    self = [super init];
    
    // This check serves to make sure that a non-NSDictionary object
    // passed into the model class doesn't break the parsing.
    if(self && [dict isKindOfClass:[NSDictionary class]]) {
            self.image = [self objectOrNilForKey:kFilesImage fromDictionary:dict];
            self.caption = [self objectOrNilForKey:kFilesCaption fromDictionary:dict];

    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation
{
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:self.image forKey:kFilesImage];
    [mutableDict setValue:self.caption forKey:kFilesCaption];

    return [NSDictionary dictionaryWithDictionary:mutableDict];
}

- (NSString *)description 
{
    return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
}

#pragma mark - Helper Method
- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict
{
    id object = [dict objectForKey:aKey];
    return [object isEqual:[NSNull null]] ? nil : object;
}


#pragma mark - NSCoding Methods

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];

    self.image = [aDecoder decodeObjectForKey:kFilesImage];
    self.caption = [aDecoder decodeObjectForKey:kFilesCaption];
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{

    [aCoder encodeObject:_image forKey:kFilesImage];
    [aCoder encodeObject:_caption forKey:kFilesCaption];
}


@end
